#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"


/********************************************************************************************/
/********************************************************************************************
    RM67120  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67120  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67120  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67120  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67120  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/
void RM67120_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}

void RM67120_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
    buffer[1]=0x03;
    SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]-1); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = buffer[3];                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<buffer[2]-1;i++)
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }
		buffer[0]=0x08;	
		buffer[1]=0x02;
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 

}

void RM67120_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    buffer1[0] = 0x06;       
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x01;                         // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0);		// Page 1	
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0035); //+1 CMD
    SSD2828_W_Reg(SigMode,channel,0xBD,0x0000);	
    SSD2828_W_Cmd(SigMode,channel,0xBF);	
    SSD2828_W_Data(SigMode,channel,((buffer[3]==0x00) ? 0xD1 : ((buffer[3]==0x42) ? 0xD2 : 0xD3))); //RED//GREEN//BLUE
    for( i = 0;i<52;i++)   //26阶灰度调整
    {	                                                                                  
        SSD2828_W_Data(SigMode,channel,buffer[i+4]);
    }
    delay_ms(5);

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok 
}

void RM67120_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u16 tmp;
    u8 i;
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    delay_ms(5);
    buffer1[0] = 0x06;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x01;                         // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0); 	

    delay_ms(5);     

    SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xC1,0x0034);
    buffer[0] = 0x01;
    buffer[1] = (buffer[3]==0x00) ? 0xD1 : ((buffer[3]==0x42) ? 0xD2 : 0xD3);         //RED//GREEN//BLUE                  
    SSD2828_W_Array(SigMode,channel,buffer,0);              
    delay_ms(5);  
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<51;i++)
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }       
    buffer[0]=0x08;	
    buffer[1]=0x09;
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);          //返回 读取的 Gamma数据  ：ok  
}


void RM67120_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
   // SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    buffer1[0] = 0x06;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x01;                         // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0);                                

    delay_ms(5);  

    buffer1[0] = 0x03;                       //gamma刻录决定 
    buffer1[1] = 0xED;
    buffer1[2] = 0x40;
    buffer1[3] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);                                

    delay_ms(5);  

    buffer1[0] = 0x04;                       
    buffer1[1] = 0xEE;
    buffer1[2] = 0xA5;
    buffer1[3] = 0x5A;
    buffer1[4] = 0x3C;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  
       
    delay_ms(2000);  //more than 500 ms
    buffer1[0] = 0x03;                       //清除烧录缓存器设定
    buffer1[1] = 0xED;
    buffer1[2] = 0x40;
    buffer1[3] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
    buffer1[3] = Uart_Error_None;
    buffer1[4] = 0;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 OTP Gamma数据  ：ok 
}

void RM67120_ID_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    //SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    buffer1[0] = 0x06;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x01;                         // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0);                                

    delay_ms(5);  

    buffer1[0] = 0x03;                       //ID刻录决定 
    buffer1[1] = 0xED;
    buffer1[2] = 0x80;
    buffer1[3] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);                                

    delay_ms(5);  

    buffer1[0] = 0x04;                       
    buffer1[1] = 0xEE;
    buffer1[2] = 0xA5;
    buffer1[3] = 0x5A;
    buffer1[4] = 0x3C;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  
       
    delay_ms(2000);  //more than 500 ms
    buffer1[0] = 0x03;                       //清除烧录缓存器设定
    buffer1[1] = 0xED;
    buffer1[2] = 0x00;
    buffer1[3] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
    buffer1[3] = Uart_Error_None;
    buffer1[4] = 0;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 OTP ID数据  ：ok 
}


void RM67120_Read_OTP_Time(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{       
    u8 buffer1[7];    
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    delay_ms(5);            
    buffer1[0] = 0x06;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x55;
    buffer1[3] = 0xAA;
    buffer1[4] = 0x52;
    buffer1[5] = 0x08;
    buffer1[6] = 0x01;                         // Page 1
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
    delay_ms(5);       
    SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);
    SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);
    buffer1[0] = 0x01;
    buffer1[1] = 0xEF;
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    delay_ms(90); 
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    SSD2828_W_Cmd(SigMode,channel,0xFA);
    buffer[4]=SPI3_Read_u8_Data(channel);
    SPI3_Read_u8_Data(channel);	
    delay_ms(5);

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[3] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 OTP次数  ：ok 

}

void ProcessForIc08( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1])
	{
			case 0x01:                                      //写寄存器
				RM67120_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x02:                                      //读寄存器
				RM67120_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x08:                                      //写Gamma数据    
				RM67120_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
			case 0x09:                                      //读Gamma数据
				RM67120_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);    break; 
			case 0x0A://gamma otp
				RM67120_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);  break;
			case 0x0B:  //回读gamma烧录次数
				RM67120_Read_OTP_Time(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); break;
			case 0x0C://ID otp
				RM67120_ID_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); break;
			default:	break;	
	}
}


